package com.itheima.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Demo01Interceptor implements HandlerInterceptor {
    /**
     * 预处理方法：在目标方法执行之前先执行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("Demo01Interceptor.preHandle.....");
        //如果方法返回true，表示放行；如果返回false，表示不放行
        return false;
    }

    /**
     * 后处理方法：在目标方法执行之后再执行（目标方法执行成功以后，才会执行）
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("Demo01Interceptor.postHandle.....");
    }

    /**
     * 最终处理方法：在给客户端返回响应之前，最终执行（无论目标方法有没有异常，都必定会执行）
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("Demo01Interceptor.afterCompletion.....");
    }
}
